<!--
 * @Description:
 * @Version: 1.0
 * @Author: liguansheng
 * @Date: 2022-08-24 15:54:09
 * @LastEditors: liguansheng
 * @LastEditTime: 2022-08-30 14:00:16
-->
<template>
  <div>
    <el-form
      :model="form"
      :rules="rules"
      ref="formElRef"
      label-width="80px"
      label-position="top"
      inline
      class="setClaimsForm"
    >
      <el-form-item
        label="类型"
        prop="type"
      >
        <el-input v-model="form.type" />
      </el-form-item>
      <el-form-item
        label="值"
        prop="value"
      >
        <el-input v-model="form.value" />
      </el-form-item>
      <el-form-item>
        <el-button
          type="success"
          :icon="Plus"
          @click="handleAdd"
        >
          添加一个新的
        </el-button>
      </el-form-item>
    </el-form>
    <el-form
      :model="form2"
    >
      <el-form-item
        label=""
        prop="type"
        style="width:100%"
      >
        <el-checkbox
          label="始终发送客户端声明"
          v-model="form2.alwaysSendClientClaims"
        />
      </el-form-item>
    </el-form>
    <ul
      class="claims"
      v-if="list.length"
    >
      <li class="title">
        <span class="type">类型</span>
        <span class="value">值</span>
      </li>
      <li
        class="secret"
        v-for="(item, index) in list"
        :key="index"
      >
        <span class="type">{{ item.type }}</span>
        <span class="value">{{ item.value }}</span>
        <el-button
          :icon="Delete"
          type="danger"
          size="small"
          @click="handleDelete(index)"
        />
      </li>
    </ul>
  </div>
</template>

<script setup lang="ts">
import { PropType, ref, watch } from 'vue'
import { Plus, Delete } from '@element-plus/icons-vue'
import { IElForm, IElFormRule } from '@/types/element-plus'
import { IClientClaim } from '@/http/types/client'

const props = defineProps({
  claims: {
    type: Array as PropType<IClientClaim[]>,
    required: true
  },
  alwaysSendClientClaims: {
    type: Boolean,
    required: true
  }
})
interface EmitsType {
  (e: 'update:claims', claims: IClientClaim[]):void
  (e: 'update:alwaysSendClientClaims', alwaysSendClientClaims: boolean):void
}
const emit = defineEmits<EmitsType>()

const formElRef = ref<IElForm|null>(null)

const form = ref<IClientClaim>({
  type: '',
  value: ''
})
const form2 = ref({
  alwaysSendClientClaims: props.alwaysSendClientClaims
})
const rules = ref<IElFormRule>({
  type: [{ required: true, message: '字段不可为空', trigger: 'blur' }],
  value: [{ required: true, message: '字段不可为空', trigger: 'blur' }]
})

const list = ref<IClientClaim[]>(props.claims)
watch(list, (newValue) => {
  emit('update:claims', newValue)
}, {
  deep: true
})
watch(() => props.claims, (newValue) => {
  list.value = newValue
}, {
  deep: true
})

watch(form2, (newValue) => {
  emit('update:alwaysSendClientClaims', newValue.alwaysSendClientClaims)
})
watch(() => props.alwaysSendClientClaims, (newValue) => {
  form2.value.alwaysSendClientClaims = newValue
})

const handleAdd = async () => {
  await formElRef.value?.validate((valid, fields) => {
    if (valid) {
      list.value.push({
        type: form.value.type,
        value: form.value.value
      })
      form.value = {
        type: '',
        value: ''
      }
    }
  })
}
const handleDelete = (index: number) => {
  list.value.splice(index, 1)
}
</script>

<style scoped lang="scss">
@import '@/styles/index.scss';

.setClaimsForm{
  .el-form-item{
    width: 45%;
    margin-bottom: 20px;
  }
}
.claims{
  span{
    display: inline-block;
    line-height: 1.5;
    font-size: 16px;
  }
  .title{
    font-weight: 600;
  }
  .secret{
    height: 40px;
    display: flex;
    align-items: center;
    &:nth-child(2n){
      background-color:  #e9e9eb;
    }
  }
  .type{
    width: 33%;
    padding-left: 10px;
    @include ellipsis(1);
  }
  .value{
    width: 60%;
    @include ellipsis(1);
  }
  .description{
    width: 20%;
  }
  .expiration{
    width: 20%
  }
}
</style>
